Xceed DataGrid for WPF v7.3 Documentation
Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.3 / Xceed DataGrid for WPF / Code Snippets / Printing and Exporting

In This Topic
    Printing and Exporting
    In This Topic

    The following page provides a list of examples that demonstrate how to customize the appearance of printable views. For more printable view-related information, refer to the Printable Views topic.

    All examples in this topic assume that the grid is bound to the Employees table of the Northwind database, unless stated otherwise.

    Configuring a print view

    The following example demonstrates how to use a PrintTableView and configure it to display a title in the page headers and the page number in the page footers. The elements added to these sections must be added as DataTemplates and will be repeated on each page.

    The Print method will be called in the button's Click event, whose implementation is provided below.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
          xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
      <Grid.Resources>
        <xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
                                        Source="{Binding Source={x:Static Application.Current},
                                                            Path=Employees}"/>        
      </Grid.Resources>
     
      <DockPanel>
        <Button Content="Print Grid"
                Click="PrintGrid"
                DockPanel.Dock="Top"/>
          <xcdg:DataGridControl x:Name="EmployeesGrid"
                                ItemsSource="{Binding Source={StaticResource cvs_employees}}">
           <xcdg:DataGridControl.PrintView>
             <xcdg:PrintTableView>
               <xcdg:PrintTableView.PageHeaders>
                 <DataTemplate>
                   <TextBlock Text="Xceed WPF Documentation"
                              HorizontalAlignment="Center"
                              FontWeight="Bold"
                              FontSize="20"/>
                 </DataTemplate>
                 <DataTemplate>
                   <TextBlock Text="Printing Example"
                              HorizontalAlignment="Center"
                              FontSize="16"/>
                 </DataTemplate>
               </xcdg:PrintTableView.PageHeaders>
               <xcdg:PrintTableView.PageFooters>
               <DataTemplate>
                 <TextBlock Text="{xcdg:ViewBinding CurrentPageNumber}"
                            HorizontalAlignment="Right"/>
               </DataTemplate>
             </xcdg:PrintTableView.PageFooters>
           </xcdg:PrintTableView>
         </xcdg:DataGridControl.PrintView>
        </xcdg:DataGridControl>
      </DockPanel>
    </Grid>
    VB.NET
    Copy Code
    Private Sub PrintGrid( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Me.EmployeeGrid.Print( "Employee_Grid", True )
    End Sub
    C#
    Copy Code
    private void PrintGrid( object sender, RoutedEventArgs e )
    {
      this.EmployeesGrid.Print( "Employee_Grid", true );
    }

    Configuring a progress window

    The following example demonstrates how to change the default text displayed in the progress window when the Print or ExportToXps methods are called. The implementation of the PrintGrid method is provided below.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
        <xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
                                        Source="{Binding Source={x:Static Application.Current},
                                                         Path=Employees}"/>
      </Grid.Resources>
      <DockPanel>
        <Button Content="Print Employee Information"
                Click="PrintGrid"
                DockPanel.Dock="Top"/>
        <xcdg:DataGridControl x:Name="EmployeesGrid"
                              ItemsSource="{Binding Source={StaticResource cvs_employees}}"
                              DockPanel.Dock="Bottom">
          <xcdg:DataGridControl.PrintView>
            <xcdg:PrintTableView>
              <xcdg:PrintTableView.ProgressWindowDescription>
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="Printing page "/>
                  <TextBlock Text="{Binding CurrentPageNumber}"/>
                  <TextBlock Text=" of employee information..."/>
                </StackPanel>
              </xcdg:PrintTableView.ProgressWindowDescription>
            </xcdg:PrintTableView>
          </xcdg:DataGridControl.PrintView>
        </xcdg:DataGridControl>
      </DockPanel>
    </Grid>
    VB.NET
    Copy Code
    Private Sub PrintGrid( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Me.EmployeesGrid.Print( "EmployeeInformation", True )
    End Sub
    C#
    Copy Code
    private void PrintGrid( object sender, RoutedEventArgs e )
    {
      this.EmployeesGrid.Print( "EmployeeInformation", true );
    }

    Styling a page

    The following example demonstrates how to create a style to change the layout of the printed pages by providing a new ControlTemplate that will place the page headers and footers at the top of each page and display an orange border around the area where the grid is printed.

    The Print method will be called in the button's Click event, whose implementation is provided below.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
          xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
      <Grid.Resources>
        <xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
                                         Source="{Binding Source={x:Static Application.Current},
                                                            Path=Employees}"/>
        <Style x:Key="page_style" TargetType="{x:Type xcdg:DataGridPageControl}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type xcdg:DataGridPageControl}">
                <DockPanel>
                  <StackPanel xcdg:DataGridPageControl.IsPageHeadersHost="True"
                              DockPanel.Dock="Top"/>
                  <StackPanel xcdg:DataGridPageControl.IsPageFootersHost="True"
                              DockPanel.Dock="Top"/>
                  <Border BorderThickness="2"
                          BorderBrush="Orange"
                          xcdg:DataGridPageControl.IsDataGridHost="True"
                          DockPanel.Dock="Bottom"/>
                </DockPanel>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Grid.Resources>
      <DockPanel>
        <Button Content="Print Grid"
                Click="PrintGrid"
                DockPanel.Dock="Top"/>
        <xcdg:DataGridControl x:Name="EmployeesGrid"
                              ItemsSource="{Binding Source={StaticResource cvs_employees}}">
          <xcdg:DataGridControl.PrintView>
           <xcdg:PrintTableView PageStyle="{StaticResource page_style}">
              <xcdg:PrintTableView.PageHeaders>
                <DataTemplate>
                  <TextBlock Text="Xceed WPF Documentation"
                             HorizontalAlignment="Center"
                             FontWeight="Bold"
                             FontSize="20"/>
                </DataTemplate>
                <DataTemplate>
                  <TextBlock Text="Printing Example"
                             HorizontalAlignment="Center"
                             FontSize="16"/>
                </DataTemplate>
              </xcdg:PrintTableView.PageHeaders>
              <xcdg:PrintTableView.PageFooters>
                <DataTemplate>
                  <StackPanel HorizontalAlignment="Right"
                              Orientation="Horizontal">
                    <TextBlock Text="Page "/>
                    <TextBlock Text="{xcdg:ViewBinding CurrentPageNumber}"/>
                  </StackPanel>  
                </DataTemplate>
              </xcdg:PrintTableView.PageFooters>
            </xcdg:PrintTableView>
          </xcdg:DataGridControl.PrintView>
        </xcdg:DataGridControl>
      </DockPanel>
    </Grid>
    VB.NET
    Copy Code
    Private Sub PrintGrid( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Me.EmployeeGrid.Print( "Employee_Grid", True )
    End Sub
    C#
    Copy Code
    private void PrintGrid( object sender, RoutedEventArgs e )
    {
     this.EmployeesGrid.Print( "Employee_Grid", true );
    }

    Exporting to Excel (ExcelExporter Class)

    The following example demonstrates how to export the content of a grid to Excel using the ExcelExporter class.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
         <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                            Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
            <xcdg:DataGridCollectionViewSource.GroupDescriptions>
               <xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
            </xcdg:DataGridCollectionViewSource.GroupDescriptions>
           
            <xcdg:DataGridCollectionViewSource.StatFunctions>
               <xcdg:AverageFunction SourcePropertyName="Freight"
                                     ResultPropertyName="average_freight" />
            </xcdg:DataGridCollectionViewSource.StatFunctions>
         </xcdg:DataGridCollectionViewSource>
      </Grid.Resources>
      <DockPanel>
         <Button Content="Export"
                 Click="ExportButton_Click"
                 DockPanel.Dock="Top" />
         <xcdg:DataGridControl x:Name="OrdersGrid"
                               ItemsSource="{Binding Source={StaticResource cvs_orders}}"
                               AutoCreateDetailConfigurations="True">
            <xcdg:DataGridControl.DefaultGroupConfiguration>
               <xcdg:GroupConfiguration>
                  <xcdg:GroupConfiguration.Footers>
                     <DataTemplate>
                        <xcdg:StatRow>
                           <xcdg:StatCell FieldName="Freight"
                                          ResultPropertyName="average_freight"/>
                        </xcdg:StatRow>
                     </DataTemplate>
                  </xcdg:GroupConfiguration.Footers>
               </xcdg:GroupConfiguration>
            </xcdg:DataGridControl.DefaultGroupConfiguration>
         </xcdg:DataGridControl>
      </DockPanel>
    </Grid>

    The following code provides the code-behind implementation of the button's Click event in which the ExcelExporter object is configured and used to export the content.

    VB.NET
    Copy Code
    Private Sub ExportButton_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Dim exporter As New ExcelExporter( Me.OrdersGrid )
      ' All details
      exporter.DetailDepth = Int.MaxValue
      ' The grid (0) and groups (1)
      exporter.StatFunctionDepth = 1
      exporter.ShowStatsInDetails = False
      exporter.ExportStatFunctionsAsFormulas = False
      exporter.Export( "d:\orders.xls" )
    End Sub
    C#
    Copy Code
    private void ExportButton_Click( object sender, RoutedEventArgs e )
    {
     ExcelExporter exporter = new ExcelExporter( this.OrdersGrid );
     // All details
     exporter.DetailDepth = int.MaxValue;
     // The grid (0) and groups (1)
     exporter.StatFunctionDepth = 1;     
     exporter.ShowStatsInDetails = false;
     exporter.ExportStatFunctionsAsFormulas = false;
     exporter.Export( "d:\\orders.xls" );
    }

    Exporting to Excel (ExportToExcel Method)

    The following example demonstrates how to export the content of a grid to Excel using the grid's ExportToExcel method.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
         <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                            Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
            <xcdg:DataGridCollectionViewSource.GroupDescriptions>
               <xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
            </xcdg:DataGridCollectionViewSource.GroupDescriptions>
         </xcdg:DataGridCollectionViewSource>
      </Grid.Resources>
      <DockPanel>
         <Button Content="Export"
                 Click="ExportButton_Click"
                 DockPanel.Dock="Top" />
         <xcdg:DataGridControl x:Name="OrdersGrid"
                               ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
      </DockPanel>
    </Grid>

    The following code provides the code-behind implementation of the button's Click event in which the grid's ExportToExcel method is called.

    VB.NET
    Copy Code
    Private Sub ExportButton_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Me.OrdersGrid.ExportToExcel( "d:\orders.xls" )
    End Sub
    C#
    Copy Code
    private void ExportButton_Click( object sender, RoutedEventArgs e )
    {
     this.OrdersGrid.ExportToExcel( "d:\\orders.xls" );
    }

    Changing the group-header formats

    The following example demonstrates how to change the text that is displayed in the group headers when they are exported to Excel.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
         <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                            Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
            <xcdg:DataGridCollectionViewSource.GroupDescriptions>
               <xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
            </xcdg:DataGridCollectionViewSource.GroupDescriptions>
         </xcdg:DataGridCollectionViewSource>
      </Grid.Resources>
      <DockPanel>
         <Button Content="Export"
                 Click="ExportButton_Click"
                 DockPanel.Dock="Top" />
         <xcdg:DataGridControl x:Name="OrdersGrid"
                               ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
      </DockPanel>
    </Grid>
    VB.NET
    Copy Code
    Private Sub ExportButton_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Dim exporter As New ExcelExporter( Me.OrdersGrid )
      exporter.GroupHeaderFormat.MultipleItemsFormat = "The {1} group contains {2} items."
      exporter.GroupHeaderFormat.SingleItemFormat = "The {1} group contains 1 item."
      exporter.Export( "d:\orders.xls" )
    End Sub
    C#
    Copy Code
    private void ExportButton_Click( object sender, RoutedEventArgs e )
    {
     ExcelExporter exporter = new ExcelExporter( this.OrdersGrid );
     exporter.GroupHeaderFormat.Plural = "The {1} group contains {2} items.";
     exporter.GroupHeaderFormat.Single = "The {1} group contains 1 item.";     
     exporter.Export( "d:\\orders.xls" );
    }